//
//  SWButton.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/22.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "SWButton.h"

@interface SWButton ()



@property (nonatomic, strong) UIButton *button;

@end

@implementation SWButton

- (UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    return _button;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imageView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = SYSTEMFONT(13);
        _titleLabel.textColor = UIColorFromRGB(0x808080);
    }
    return _titleLabel;
}

+ (instancetype)buttonWithImage:(NSString *)image title:(NSString *)title target:(id)target action:(SEL)action{
    
    SWButton *backButton = [[SWButton alloc] init];
    
    backButton.imageView.image = [UIImage imageNamed:image];
    backButton.titleLabel.text = title;
    [backButton.button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [backButton makeSubViews];
    
    return backButton;
}

- (void)makeSubViews{
    
    [self addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(self);
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(10);
    }];
    
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.imageView.mas_right).with.offset(5);
        make.right.equalTo(self).with.offset(-10);
        make.centerY.equalTo(self.imageView);
        make.height.equalTo(self.imageView);
//        make.width.mas_equalTo(100);
        
    }];
    
    [self addSubview:self.button];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

- (void)layoutSubviews{
    [super layoutSubviews];
//    self.titleLabel.width = self.titleLabel.text.length*18;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
